// 缓存分析


export const BQGIS_buffer = {
    defaultPointOption: {
        type: "point",
        hasEdit: false,
        style: {
            pixelSize: 12,
            color: "#ffff00"
        },
        attr: {
            width: 1000,
            color: "rgba(255,0,0,0.4)"
        }
    },
    defaultPolylineOption: {
        type: "polyline",
        hasEdit: false,
        style: {
            color: "#ffff00",
            width: 3,
            clampToGround: true
        },
        attr: {
            width: 1000,
            color: "rgba(255,0,0,0.4)"
        }
    },
    defaultPolygonOption: {
        type: "polygon",
        hasEdit: false,
        style: {
            color: "#ffff00",
            outline: true,
            outlineColor: "#f0ce22",
            outlineWidth: 4,
            opacity: 0.5,
            clampToGround: true
        },
        attr: {
            width: 1000,
            color: "rgba(255,0,0,0.4)"
        }
    },
    /**
     *
     * 初始化
     *
     * @param    viewer  Mars3d实例对象
     *
     */
    init(viewer = window.viewer) {
        if (!viewer.bq_bufferLayer) {
            viewer.bq_bufferLayer = new mars3d.layer.GraphicLayer({
                hasContextMenu: false,
                hasTooltip: false,
                hasEdit: false,
                isAutoEditing: false // 绘制完成后是否自动激活编辑
            })
            viewer.addLayer(viewer.bq_bufferLayer)
        }
        if (!viewer.bq_originalBufferLayer) {
            viewer.bq_originalBufferLayer = new mars3d.layer.GraphicLayer({
                hasEdit: true,
                isAutoEditing: false // 绘制完成后是否自动激活编辑
            })
            viewer.addLayer(viewer.bq_originalBufferLayer)
        }
        return viewer.bq_originalBufferLayer
    },
    /**
     *
     * 绘制点线面，生成缓存分析
     *
     * @param    option  如果不传，则使用上方的默认参数(default), 参数可参考 mars3d 的 PointEntity，PolylineEntity，PolygonEntity，
     *                   attr中的width和color，表示缓冲区宽度和缓冲区颜色
     *
     */
    drawBuffer(option, viewer = window.viewer) {
        return viewer.bq_originalBufferLayer.startDraw(option).then(graphic => {
            this.addBufferBygraphicsOptions(this.createBuffer(graphic), viewer)
            graphic.bindContextMenu([{
                text: '删除对象',
                icon: 'fa fa-trash-o',
                callback: function (e) {
                    let graphic = e.graphic
                    if (graphic) {
                        graphic.remove(true)
                    }
                },
            }])
            graphic.on([mars3d.EventType.updatePosition, mars3d.EventType.updateAttr], function (e) {
                const buffer = viewer.bq_bufferLayer.getGraphicById(e.graphic.id)
                buffer.remove(true)
                this.addBufferBygraphicsOptions(this.createBuffer(e.graphic), viewer)
            }, this)
            graphic.on(mars3d.EventType.remove, function (e) {
                const buffer = viewer.bq_bufferLayer.getGraphicById(e.graphic.id)
                if (buffer) {
                    buffer.remove(true)
                }
            }, this)
            return {
                graphic,
                graphics: viewer.bq_originalBufferLayer.graphics
            }
        })
    },
    createBuffer(
        graphic,
    ) {
        let buffere
        try {
            if (!graphic) return
            const geojson = graphic.toGeoJSON()
            geojson.properties = {}

            buffere = mars3d.PolyUtil.buffer(geojson, graphic.attr.width)
        } catch (e) {
            console.log("缓冲分析异常", e)
        }
        if (!buffere) {
            return
        }

        const graphicsOptions = mars3d.Util.geoJsonToGraphics(buffere, {
            type: "polygon",
            style: {
                color: graphic.attr.color,
                clampToGround: true
            }
        })
        graphicsOptions[0].id = graphic.id
        return graphicsOptions
    },
    clearAll(viewer = window.viewer) {
        viewer.bq_originalBufferLayer.clear()
        viewer.bq_bufferLayer.clear()
    },
    deleteById(id, viewer = window.viewer) {
        const graphic = viewer.bq_originalBufferLayer.getGraphicById(id)
        graphic.remove(true)
        return {
            graphic,
            graphics: viewer.bq_originalBufferLayer.graphics
        }
    },
    addBufferBygraphicsOptions(graphicsOptions, viewer = window.viewer) {
        const buffer = viewer.bq_bufferLayer.addGraphic(graphicsOptions)[0]
        buffer.bindContextMenu([{
            text: '删除对象',
            icon: 'fa fa-trash-o',
            callback: function (e) {
                if (e.graphic) {
                    const graphic = viewer.bq_originalBufferLayer.getGraphicById(e.graphic.id)
                    graphic.remove(true)
                }
            },
        }])
        return buffer
    },
}